package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.accesservice.dao.IDaoDeviceLogin;
import com.ocom.accesservice.service.CompanyListInfoService;
import com.ocom.accesservice.service.DeviceLoginService;
import com.ocom.accesservice.vo.devicelogin.GetLoginAccountVo;
import com.ocom.common.entity.master.EntityCompanyList;
import com.ocom.common.entity.master.EntityDeviceLogin;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DeviceLoginServiceImpl extends ServiceImpl<IDaoDeviceLogin, EntityDeviceLogin> implements DeviceLoginService {

    @Autowired
    CompanyListInfoService companyListInfoService;

    @Autowired
    private IDaoDeviceLogin daoDeviceLogin;
    /**
     * 获取登录账号（getLoginAccount）
     *
     * @return*/
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityDeviceLogin> GetLoginAccount(GetLoginAccountVo getLoginAccountVo) {
        QueryWrapper<EntityDeviceLogin> query = Wrappers.query();

        if (getLoginAccountVo.getComId()!=null && getLoginAccountVo.getComId()>=0){
            query.eq("com_id",getLoginAccountVo.getComId());
        }
        if (getLoginAccountVo.getAgentId()>1){
            List<EntityCompanyList> lists=companyListInfoService.FindMachComId(getLoginAccountVo.getAgentId().intValue());
            List<Long> orderNoList=lists.stream().map(EntityCompanyList::getComId).collect(Collectors.toList());
            orderNoList.add(getLoginAccountVo.getAgentId());
            query.in("com_id",orderNoList);
        }

        if (StringUtils.isNotEmpty(getLoginAccountVo.getDeviceNo())){
            query.eq("device_no",getLoginAccountVo.getDeviceNo());
        }
        if (StringUtils.isNotNull(getLoginAccountVo.getLoginType())){
            query.eq("login_type",getLoginAccountVo.getLoginType());
        }
        if (StringUtils.isNotEmpty(getLoginAccountVo.getAccount())){
            query.like("account",getLoginAccountVo.getAccount());
        }
        if (StringUtils.isNotEmpty(getLoginAccountVo.getSender())){
            query.like("sender",getLoginAccountVo.getSender());
        }
        PageHelper.startPage(getLoginAccountVo.getPage(), getLoginAccountVo.getSize());
        return daoDeviceLogin.selectList(query);
    }

    /**
     * 修改登录账号（updateLoginAccount）【WEB】
     * */
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void UpdateLoginAccount(EntityDeviceLogin deviceLogin) {
        daoDeviceLogin.updateById(deviceLogin);
    }
    /**
     * 新增登录账号（addLoginAccount）【WEB】
     * */
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void AddLoginAccount(EntityDeviceLogin deviceLogin) {
        daoDeviceLogin.insert(deviceLogin);
    }

    /**
     * 删除登录账号（deleteLoginAccount）【WEB】
     *
     * @return*/
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public EntityDeviceLogin DeleteLoginAccount(Long comId, Long id) {
        QueryWrapper<EntityDeviceLogin> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("id",id);
        EntityDeviceLogin entityDeviceLogin = daoDeviceLogin.selectOne(query);
        daoDeviceLogin.delete(query);
       return entityDeviceLogin;
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    public EntityDeviceLogin GetLoginAccount(Long comId) {
        QueryWrapper<EntityDeviceLogin> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("login_type",2);
        query.last("limit 1");
        return this.getOne(query);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityDeviceLogin> GetMachNoRandon(Long comId) {
        QueryWrapper<EntityDeviceLogin> query = Wrappers.query();
//        query.eq("com_id",comId);
        query.eq("login_type",2);
        return  this.list(query);
//        if(list==null || list.size()==0){
//            return null;
//        }
//        Random random = new Random();
//        int num = random.nextInt(list.size());
//        return list.get(num);
    }
}
